
package com.productmodule.servlet;
import com.productmodule.bean.TbProductType;
import com.productmodule.dao.TbProductTypeDAO;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import util.*;

public class TbProductTypeServlet extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        if("add".equals(method)){
        String productTypeName=request.getParameter("productTypeName");
        int parentTypeId=Integer.parseInt(request.getParameter("parentTypeId"));
        String productTypeMemo=request.getParameter("productTypeMemo");
            if(new TbProductTypeDAO().add(new TbProductType(productTypeName,parentTypeId,productTypeMemo))){
            request.getSession().setAttribute("result", "添加成功");
            }else{            
            request.getSession().setAttribute("result", "添加失败");
            }
            goList(request, response);
        }
        if("del".equals(method)){
            if(UtilDAO.delin("Tb_ProductType", "ProductTypeId", request.getParameter("delIds"))){   
            request.getSession().setAttribute("result", "删除成功");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
            goList(request, response);
        }
        if("edit".equals(method)){
        int productTypeId=Integer.parseInt(request.getParameter("productTypeId"));
        String productTypeName=request.getParameter("productTypeName");
        int parentTypeId=Integer.parseInt(request.getParameter("parentTypeId"));
        String productTypeMemo=request.getParameter("productTypeMemo");
            if(new TbProductTypeDAO().edit(new TbProductType(productTypeId,productTypeName,parentTypeId,productTypeMemo))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
            goList(request, response);
        }
        if ("list".equals(method))goList(request, response);
        if ("listById".equals(method)) {
        	request.getSession(true).setAttribute("tbProductType",new TbProductTypeDAO().getTbProductTypeByProductTypeId(Integer.parseInt(request.getParameter("productTypeId"))));
            response.sendRedirect("ProductModule/TbProductTypeOperate.jsp");
    	}
        if("showadd".equals(method)){
            request.getSession(true).removeAttribute("tbProductType");
            response.sendRedirect("ProductModule/TbProductTypeOperate.jsp");
        }
      
    }
    public void goList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.getSession().setAttribute("tbProductTypeList",new TbProductTypeDAO().getTbProductTypeAll());
        response.sendRedirect("ProductModule/TbProductTypeList.jsp");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doGet
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doPost
}
